﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Globalization;

public partial class Manage_NewArticle : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //加载模版信息
            var fs = from aF in new DirectoryInfo(Server.MapPath("~/Templates")).GetFiles()
                     where aF.Name.EndsWith(@".aspx")
                     select new { Name = aF.Name.Replace(@".aspx", "") };
            TemplateList.DataSource = fs;
            TemplateList.DataTextField = "Name";
            TemplateList.DataValueField = "Name";
            TemplateList.DataBind();


            //加载已经存在的文章信息
            string aid = Request.Params["aid"];
            if (aid != null && aid != "")
            {
                TreasurerDataContext db = new TreasurerDataContext();
                var q = from aA in db.Articles
                        where aA.ID == aid
                        select aA;

                if (q.Count() != 0)
                {
                    Articles a = q.First();
                    ArticleID.Value = a.ID;
                    ArticleTitle.Text = a.Title;
                    Subtitle.Text = a.Subtitle;
                    Keywords.Text = a.Keyword;
                    ForumList.SelectedValue = a.ForumID;
                    Body.Text = a.Body;
                    TemplateList.SelectedValue = a.Template;
                    Published.Text = string.Format("{0:yyyy/MM/dd HH:mm:ss}", a.Published);
                }
            }
        }
    }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        Articles a;
        TreasurerDataContext db = new TreasurerDataContext();

        if (ArticleID.Value.Trim() == "")
        {
            a = new Articles();
            db.Articles.InsertOnSubmit(a);

            a.ID = Toolkit.NextId();
            a.Published = DateTime.Now;
            a.Clicked = 0;
        }
        else
        {
            a = (from aA in db.Articles
                 where aA.ID == ArticleID.Value.Trim()
                 select aA).First();
        }

        a.ForumID = ForumList.SelectedValue;
        a.UserID = (string)Session["Current_User_id"];
        a.Title = ArticleTitle.Text.Trim();
        a.Subtitle = Subtitle.Text.Trim();
        a.Body = Body.Text.Trim();
        a.Keyword = Keywords.Text.Trim();
        a.Template = TemplateList.SelectedValue;
        a.Url = a.ID + ".html";

        try
        {
            a.Published = DateTime.ParseExact(Published.Text.Trim(), "yyyy/MM/dd HH:mm:ss", null);
        }
        catch (Exception exp)
        {
        }


        db.SubmitChanges();


        #region 生成静态页面
        string dst = Server.MapPath(string.Format(@"~/Statics/{0}.html", a.ID));

        //如果目标文件存在，则删除
        if (File.Exists(dst))
        {
            File.Delete(dst);
        }

        //获取原文件输入流
        TextWriter tw = new StringWriter();
        Server.Execute(string.Format(@"~/Templates/{0}.aspx?aid={1}", a.Template, a.ID), tw);

        StreamWriter writer = new StreamWriter(dst, false, System.Text.Encoding.UTF8);
        writer.Write(tw.ToString());

        writer.Close();
        tw.Close();
        #endregion


        Response.Write("<script>alert('文章保存成功。');document.location='ListArticles.aspx';</script>");
    }
}